package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.bean.Student;
import com.itheima.bean.User;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentServiceImpl implements StudentService{

    // 分页查询
    @Override
    public Page selectByPage(Integer currentPage, Integer pageSize) {
        InputStream is = null;
        SqlSession sqlSession = null;
        Page page = null;
        try {
            // 加载驱动
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 获取工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 获取接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            // 设置分页参数
            page = PageHelper.startPage(currentPage, pageSize);
            // 调用方法
            mapper.selectAll();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 返回结果
        return page;
    }

    // 添加
    @Override
    public void addStu(Student stu) {
        InputStream is = null;
        SqlSession sqlSession = null;
        try {
            // 加载驱动
             is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 获取工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 获取接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            // 调用方法
            mapper.addStu(stu);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    // 修改
    @Override
    public void updateStu(Student stu) {
        InputStream is = null;
        SqlSession sqlSession = null;
        try {
            // 加载驱动
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 获取工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 获取接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            // 调用方法
            mapper.updateStu(stu);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 删除
    @Override
    public void deleteStu(String number) {
        InputStream is = null;
        SqlSession sqlSession = null;
        try {
            // 加载驱动
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 获取工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 获取接口实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            // 调用方法
            mapper.deleteStu(number);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
